package com.xiaochong.bitong.api.service;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.vo.BtUser;
import com.xiaochong.tir.common.util.RedisKeyApp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.bitong.api.service
 * @Description : TODO 公共服务
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年08月10日下午11:33
 * @ModificationHistory Who When What
 */
@Service
public class CommonService {

    @Autowired
    OSSTool ossTool;

    @Autowired
    RedisServer redisServer;

    /**
     * 文件上传
     * @param file
     * @return
     * @throws Exception
     */
    public String  uploadFile( MultipartFile file , int userId) throws Exception {

        String prefix = "png";

        prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileName = userId+"_"+UUID.randomUUID().toString().replace("-", "") + "." + prefix;

        boolean imgFlag = ossTool.upload(  file.getInputStream(),fileName );
        if (imgFlag==true){
            LogTrace.infoKvs("上传图片成功","userId",userId+"","imgurlKey", fileName);
        }else{
            LogTrace.infoKvs("上传图片失败","userId",userId+"","imgurlKey", fileName);
        }

       return fileName;
    }

    /**
     * 根据Token 获取用户信息
     * @param token
     * @return
     */
    public BtUser getUserByToken(String token ){
        if(StringUtils.isNotBlank(token)){
            String userToken= RedisKeyApp.USER_TOKEN+token;

            return redisServer.get(userToken , BtUser.class);
        }else {
            return  null;
        }
    }

    /**
     * 刷新用户缓存
     * @param token
     * @param btUser
     */
    public void refreshUserToken(String token , BtUser btUser){
        if(StringUtils.isNotBlank(token)){
            String redisKey = RedisKeyApp.USER_TOKEN+token;
            BtUser cacheUser = redisServer.get(redisKey , BtUser.class);
            if(cacheUser == null){
                cacheUser = new BtUser();
            }
            if(btUser.getCountryNum() != null) {
                cacheUser.setCountryNum(btUser.getCountryNum());
            }
            if(btUser.getCreateTime() != null) {
                cacheUser.setCreateTime(btUser.getCreateTime());
            }
            if(btUser.getEmail() != null) {
                cacheUser.setEmail(btUser.getEmail());
            }
            if(btUser.getId() != null) {
                cacheUser.setId(btUser.getId());
            }
            if(btUser.getImageUrl() != null) {
                cacheUser.setImageUrl(btUser.getImageUrl());
            }
            if(btUser.getMobile() != null) {
                cacheUser.setMobile(btUser.getMobile());
            }
            if(btUser.getUserName() != null) {
                cacheUser.setUserName(btUser.getUserName());
            }
            if(btUser.getUpdateTime() != null) {
                cacheUser.setUpdateTime(btUser.getUpdateTime());
            }
            if(btUser.getSource() != null) {
                cacheUser.setSource(btUser.getSource());
            }
            redisServer.set(redisKey ,cacheUser);
        }
    }
}
